package com.itheima.练习.test6;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyRunnable implements Runnable{
    Socket socket;
    public MyRunnable(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        try {
            String str= UUID.randomUUID().toString().replace("-","");

            BufferedInputStream bis=new BufferedInputStream(socket.getInputStream());

            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("E:\\java lianxi\\Socket\\src\\main\\111"+str+".png"));

            byte[] bytes=new byte[2048];
            int b;
            while ((b=bis.read(bytes))!=-1){
                bos.write(bytes,0,b);
            }

            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();



        } catch (IOException e) {
            throw new RuntimeException(e);

        }finally {
            if(socket!=null){
            try {
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            }
        }
    }
}
